package com.mac.bluetoothbt.feature;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.math.BigDecimal;
import java.security.InvalidParameterException;
import java.util.Locale;

/**
 * @author wyh
 * 绘制主界面试管
 */
public class CylinderView extends TableLayout {
    TextView top, head, body, tail;
    Context oContext;

    public CylinderView(Context context, int width) {
        this(context, null, width);
    }

    public CylinderView(Context context, AttributeSet attrs, int width) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.cylinder_view, this, true);
        top = this.findViewById(R.id.cylinder_view_top);
        head = this.findViewById(R.id.cylinder_view_head);
        body = this.findViewById(R.id.cylinder_view_body);
        tail = this.findViewById(R.id.cylinder_view_tail);
        body.setWidth(width);
        oContext = context;
    }

    @ColorInt public static final int GREEN = 0xFF006400;
    public void fill(Sensor sensor) {
        head.setText(sensor.getGasClassString());
        Drawable toppic = getResources().getDrawable(R.mipmap.top, null).mutate();


        int gasIndex = sensor.getGasIndex();
        if (gasIndex > 0) {
            Log.d("debug", "fill: " + gasIndex);
        }
        ColorFilter cf = FrameUtil.getGasColor(gasIndex);
        toppic.setColorFilter(cf);
        top.setBackground(toppic);


        TextView pressureview = this.findViewById(R.id.perssurenumber);
        BigDecimal dispVal = new BigDecimal(((Number)sensor.getDisplayVal()).toString());

        pressureview.setText(dispVal.setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
        body.setBackground(getColumnOfContent(sensor));
        int state = sensor.getState();
        tail.setText(FrameUtil.getState(state));
        top.setText(getSpanned(state, sensor));

        if (state == 0) {
            pressureview.setTextColor(GREEN);
            tail.setTextColor(GREEN);
        } else {
            pressureview.setTextColor(Color.RED);
            tail.setTextColor(Color.RED);
        }
    }


    private Spanned getSpanned(int state, Sensor sensor) {
        Spanned topSpanned;
        String topstring;

        if (state == 0) {
            topstring = FrameUtil.getAbb(sensor.getGasIndex());
        } else {
            topstring = FrameUtil.getAbb(sensor.getGasIndex());
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            topSpanned = Html.fromHtml(topstring, Html.FROM_HTML_MODE_LEGACY);
        } else {
            topSpanned = Html.fromHtml(topstring);
        } return topSpanned;
    }

    private static final int[] COLUMNS = {R.mipmap.f01, R.mipmap.f02, R.mipmap.f03, R.mipmap.f04, R.mipmap.f05, R.mipmap.f06, R.mipmap.f07, R.mipmap.f08, R.mipmap.f09, R.mipmap.f10};

    private Drawable getColumnOfContent(Sensor sensor) {
        if (sensor.getState() == 1) {
            return oContext.getDrawable(COLUMNS[0]);
        } else if (sensor.getState() == 2) {
            return oContext.getDrawable(COLUMNS[COLUMNS.length - 1]);
        }

        float displayVal = sensor.getDisplayVal();
        float min = sensor.getPressureMin();
        float max = sensor.getPressureMax();
        float diff = max - min;
        diff /= 10;
        if (displayVal <= min) {
            return oContext.getDrawable(COLUMNS[0]);
        } else if (displayVal >= max) {
            return oContext.getDrawable(COLUMNS[COLUMNS.length - 1]);
        }
        for (int i = 1; i < 9; i++) {
            if (displayVal < min + diff * i) {
                return oContext.getDrawable(COLUMNS[i]);
            }
        }

        return oContext.getDrawable(COLUMNS[COLUMNS.length - 1]);
    }
}
